package com.nkym.wypan.controller;/*
 * @ClassName UserController
 * @Desc TODO
 * @Author 19637
 * @Date 2023/6/5 22:40
 * @Version 1.0
 */

import com.nkym.wypan.annotation.LoginIgnore;
import com.nkym.wypan.model.entity.UserInfo;
import com.nkym.wypan.model.req.user.LoginReq;
import com.nkym.wypan.model.req.user.UserRegisterReq;
import com.nkym.wypan.model.resp.LoginResp;
import com.nkym.wypan.service.UserService;
import com.nkym.wypan.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/register")
    @ApiOperation(
            value = "用户注册",
            notes = "幂等可并发的注册接口"
    )
    @LoginIgnore
    public BaseResponse register(
            @RequestBody @Valid
            UserRegisterReq userRegisterReq) {
        userService.register(userRegisterReq);
        return BaseResponse.okPutMessage("注册成功");
    }

    @PostMapping("/login")
    @ApiOperation(
            value = "用户登录",
            notes = "幂等可并发的登陆接口"
    )
    @LoginIgnore
    public BaseResponse<LoginResp> login(@RequestBody @Valid
                                         LoginReq loginReq) {
        LoginResp resp = new LoginResp();
        String token = userService.login(loginReq);
        resp.setToken(token);
        return BaseResponse.ok(resp);
    }

    @PostMapping("/user-info")
    @ApiOperation(
            value = "获取获取用户信息"
    )
    public BaseResponse<UserInfo> getUserInfo() {
        UserInfo userInfo = userService.getUserInfo();
        return BaseResponse.ok(userInfo);
    }

    @PostMapping("/logout")
    @ApiOperation(
            value = "用户退出"
    )
    public BaseResponse logout() {
        userService.logout();
        return BaseResponse.ok();
    }


}
